<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Storage;
use DB;
use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Core\OssUtil;
/**
 * Class Banner
 *
 * @package App
 * @mixin \Eloquent
 */
class Gamepic extends Model
{
    public $timestamps = false;
    protected $table = 'gave_pic';

    /**
     * 添加游戏图片
     * @param $request
     * @return array
     */
    static function picPublish($request)
    {
		DB::beginTransaction();
        $picInsert = [];
        for ($i = 1; $i <= 20; $i++) {
			$feild = 'purl' . $i;
			if ($request->has($feild)) {
				$extension = $request->$feild->extension();
                if (!in_array($extension, ['png', 'jpg', 'jpeg', 'bmp', 'tiff', 'gif'])) {
                    DB::rollBack();
                    return ['status' => 2, 'message' => '图片格式不正确'];
                }
				
				//上传图片
				$path = Storage::url(self::OssUpdate($request->$feild));

				 $picInsert[] = [
					 'path' => $path
				 ];
			}		 
            
        }
		
        //数据库添加数据
        $pic_insert = DB::table('gave_pic')->insert($picInsert);
        if (!$pic_insert) {
            DB::rollBack();

            return ['status' => 2, 'message' => '图片上传失败'];
        }
		DB::commit();

        return ['status' => 1, 'message' => '图片上传成功'];
		
    }

    static function OssUpdate($file)
    {
        return $file->store('/', 'oss');
    }
}
